package com.gzcc.config;

import com.gzcc.core.security.DbUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
* 描述: security配置类
* @Author: Mr.Zhong
* @Date: 2018/6/13
*/

@Configuration
@EnableWebSecurity()
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private DbUserDetailsService userDetailsService;

    @Override
    public UserDetailsService userDetailsServiceBean() throws Exception {
        return userDetailsService;
    }

    /**
    * 描述: 密码加密
    * @Author: Mr.Zhong
    * @Date: 2018/6/13
    */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
    * 描述: security配置访问路径
    * @Author: Mr.Zhong
    * @Date: 2018/6/13
    */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();
        http
                .authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/consumer/**").hasRole("CONSUMER")
                .antMatchers("/order/**").permitAll()
                .antMatchers("/front/**").permitAll()
                .antMatchers("/api/**").permitAll()
                .antMatchers("/upload/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .headers().frameOptions().sameOrigin()
                .and()
                .formLogin()
                .loginPage("/login")
                .successHandler((httpServletRequest, httpServletResponse, authentication) -> httpServletResponse.sendRedirect("/"))
                .failureHandler((httpServletRequest, httpServletResponse, e) -> {
                    httpServletRequest.getSession().setAttribute("message", "用户名或者密码有误！");
                    httpServletRequest.getRequestDispatcher("/login").forward(httpServletRequest, httpServletResponse);
                })
                .permitAll()
                .and()
                .logout()
        .permitAll();
        http
                .csrf().disable();
    }

    /**
    * 描述: security忽略拦截静态资源
    * @Author: Mr.Zhong
    * @Date: 2018/6/13
    */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().mvcMatchers("/assets/**", "/vendors/**","/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
    }

}
